/*
 * Copyright (C) 2008 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package ch.lab24.android.rtm;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;

import ch.lab24.android.rtm.provider.RTM;

import android.content.Context;
import android.content.res.Resources;
import android.database.Cursor;
import android.graphics.Color;
import android.graphics.PorterDuff;
import android.view.View;
import android.widget.ImageView;
import android.widget.ResourceCursorAdapter;
import android.widget.TextView;

public class TaskListAdapter extends ResourceCursorAdapter {

	private Resources mResources;

	public TaskListAdapter(Context context, int resource) {
		super(context, resource, null);
		mResources = context.getResources();
	}

	void refresh() {
		getCursor().requery();
	}

	@Override
	public void bindView(View view, Context context, Cursor cursor) {
		ImageView stripe = (ImageView) view.findViewById(R.id.vertical_stripe);
		// ToDo: Color
		// int color = cursor.getInt(AgendaActivity.INDEX_COLOR) & 0xbbffffff;
		int priority = cursor.getInt(cursor.getColumnIndex(RTM.Tasks.PRIORITY));
		int color = 0;
		if (priority == 1)
			color = Color.RED;
		else if (priority == 2)
			color = Color.BLUE;
		else if (priority == 2)
			color = Color.CYAN;
		stripe.getBackground().setColorFilter(color, PorterDuff.Mode.SRC_IN);

		// What
		TextView title = (TextView) view.findViewById(R.id.title);
		String titleString = cursor.getString(cursor
				.getColumnIndex(RTM.Tasks.NAME));
		if (titleString == null || titleString.length() == 0) {
			titleString = mResources.getString(R.string.no_title_label);
		}
		title.setText(titleString);

		// Time
		TextView time = (TextView) view.findViewById(R.id.time);
		int hasTime = cursor.getInt(cursor.getColumnIndex(RTM.Tasks.HAS_DUE));
		if (hasTime > 0) {
			final SimpleDateFormat format = new SimpleDateFormat("HH:mm");
			format.setTimeZone(TimeZone.getDefault());
			long dueDate = cursor.getLong(cursor.getColumnIndex(RTM.Tasks.DUE));
			time.setVisibility(View.VISIBLE);
			time.setText(format.format(new Date(dueDate)));
		} else {
			time.setVisibility(View.GONE);
		}

		// Repeating info
		View repeatContainer = view.findViewById(R.id.notes_icon);
		int hasNotes = cursor
				.getInt(cursor.getColumnIndex(RTM.Tasks.HAS_NOTES));
		if (hasNotes > 0) {
			repeatContainer.setVisibility(View.VISIBLE);
		} else {
			repeatContainer.setVisibility(View.GONE);
		}

		// Where
		TextView where = (TextView) view.findViewById(R.id.tags);
		String whereString = cursor.getString(cursor
				.getColumnIndex(RTM.Tasks.TAGS));
		if (whereString != null && whereString.length() > 0) {
			where.setVisibility(View.VISIBLE);
			where.setText(whereString);
		} else {
			where.setVisibility(View.GONE);
		}

		// Lists
		TextView lists = (TextView) view.findViewById(R.id.lists);
		String listString = null;
		if (listString != null && listString.length() > 0) {
			lists.setVisibility(View.VISIBLE);
			lists.setText(listString);
		} else {
			lists.setVisibility(View.GONE);
		}

	}

}
